import logging
logger = logging.getLogger('F:\AI_BASIC/ai_-basic/AI_Basic_lab/RegrassionAndPredict/7plt_pie.py')
addHandler = logging.FileHandler('F:\AI_BASIC/ai_-basic/AI_Basic_lab/RegrassionAndPredict/7plt_pie.py.log')
logger.addHandler(addHandler)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
addHandler.setFormatter(formatter)


logger.setLevel(logging.INFO)
logger.info('This is a log message')


import pandas as pd
# 二手房数据
house_price_df = pd.read_csv('F:/AI_BASIC/ai_-basic/AI_Basic_lab/RegrassionAndPredict/bj_house_information.csv')

#册除一些不重要的列
to_drop = ['Id', '朝向', '电梯', '装修', '楼层', '小区名称', '地点', '楼龄']
house_price_df_clean = house_price_df.drop(to_drop, axis=1)
# 显示列名
print(house_price_df_clean.columns)
print(house_price_df_clean.head())


# 重新摆放列位置
columns = ['房屋总价', '建筑面积', '区域','户型']
house_price_df_clean = pd.DataFrame(house_price_df_clean, columns = columns)
print(house_price_df_clean.head())

lianjia_total_num = house_price_df_clean['建筑面积'].count()
print('房价数据集总数量为: ' + str(lianjia_total_num))

#数据清洗
df = house_price_df_clean
df['房屋单价'] = df['房屋总价']/df['建筑面积']
# 对汇总数据再次清洗
df.dropna(how='any')
df.drop_duplicates(keep='first', inplace=True)
# 一些别墅的房屋单价有异常，删选价格少于25万一平的
df = df.loc[df['房屋单价']<25]

# 对二手房区域分组对比二手房数量和每平米房价
df_house_count = df.groupby('区域')['房屋总价'].count().sort_values(ascending=False)
df_house_mean = df.groupby('区域')['房屋单价'].mean().sort_values(ascending=False)

import matplotlib.pyplot as plt
plt.rc('font', family='SimHei', size=13)
plt.style.use('ggplot')
plt.figure(figsize=(10, 10))
plt.title(u'各区域二手房数量百分比', fontsize=18)
explode = [0] * len(df_house_count)
explode[0] = 0.2
plt.pie(df_house_count, radius=3, autopct='%1.f%%', shadow=True, labels=df_house_count.index)
plt.axis('equal')

plt.show()

logger.info('Enging  plt_pie')



